package com.jetpack.jetpacknavtest

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.jetpack.jetpacknavtest.databinding.Fragment1Binding
import kotlinx.android.synthetic.main.fragment_1.*


/**
 * A simple [Fragment] subclass.
 */
class Fragment1 : Fragment() {

    lateinit var myViewModel: MyViewModel
    lateinit var mBinding: Fragment1Binding


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_1, container, false)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        // activity 写法
//        myViewModel = ViewModelProvider(this, ViewModelProvider.AndroidViewModelFactory.getInstance(application)).get(MyViewModel::class.java)
        // fragment 写法
        myViewModel= ViewModelProvider(activity!!,ViewModelProvider.AndroidViewModelFactory.getInstance(activity!!.application)).get(MyViewModel::class.java)
        mBinding.viewmodel = myViewModel
        mBinding.lifecycleOwner = activity
        Log.e("fragment", "1")
        go.setOnClickListener {

            var action = Fragment1Directions.actionFragment1ToFragment2("sssss");

            Navigation.findNavController(it).navigate(R.id.action_fragment1_to_fragment2)

        }
        back.setOnClickListener {
            Navigation.findNavController(it).navigateUp()
        }

//        button.setOnClickListener {
//            Navigation.findNavController(it).navigateUp()
//        }


//        myViewModel.name.observe(this, Observer {
//
//        })

    }

}
